<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >申报
        </el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
        >批量导入
        </el-button>
      </el-col>


      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:awards:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="awardsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="奖项名称" align="center" prop="name"/>
      <el-table-column label="角色" align="center" prop="isFirstPerson" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.is_first_person" :value="scope.row.isFirstPerson"/>
        </template>
      </el-table-column>
      <el-table-column label="积分" align="center" prop="points" width="90"/>
      <el-table-column label="上传人" align="center" prop="userName" width="100"/>
      <el-table-column label="审核人" align="center" prop="check1Name" width="100"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {hh}:{mm}:{ss}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="图片" align="center" prop="pic" width="120">
        <template slot-scope="scope">
          <image-preview :src="scope.row.pic" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="state" width="120">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.project_state" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="280" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-sort"
            @click="handleCheck(scope.row)"
            v-if="scope.row.state == 0"
            v-hasPermi="['project:achievement:edit']"
          >送审
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleDetail(scope.row)"
            v-hasPermi="['project:demand:list']"
          >查看详情
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handSeeCheck(scope.row)"
            v-if="scope.row.state != 0"
          >查看审核详情
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:awards:edit']"
            v-if="scope.row.state == 0"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:awards:remove']"
            v-if="scope.row.state == 0"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 查看详情对话框 -->
    <el-dialog :title="title" :visible.sync="detailOpen" width="780px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="荣誉名称:">{{ form.name }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="积分:">{{ form.points }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="创建时间:">{{ form.createTime }}</el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关闭</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title" :visible.sync="seeCheckOpen" width="800px" append-to-body>
      <el-steps :active="getActiveStep(checkForm.checkResult)" class="custom-steps">
        <el-step :title="getStepTitle(checkForm.checkResult, 0)"></el-step>
        <el-step :title="getStepTitle(checkForm.checkResult, 1)"></el-step>
        <el-step :title="getStepTitle(checkForm.checkResult, 2)"></el-step>
      </el-steps>


      <el-form ref="checkForm" :model="checkForm" :rules="rules" label-width="100px">
        <el-row style="margin-top: 20px">
          <el-col :span="12">
            <span>审核人： 胡昊，吴兆发</span>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关闭</el-button>
      </div>
    </el-dialog>


    <!-- 添加或修改奖项管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form :model="querySceneParams" ref="querySceneForm" size="small" :inline="true" v-show="showSearch1"
               label-width="68px">
        <el-form-item label="涉及事项" prop="type">
          <el-select v-model="querySceneParams.type" placeholder="请选择涉及事项">
            <el-option
              v-for="dict in dict.type.awards_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery1">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery1">重置</el-button>
        </el-form-item>


        <el-table v-loading="loading1" :data="sceneList">
          <el-table-column label="编号" align="center" width="55" prop="id"/>
          <el-table-column label="奖项名称" align="center" prop="scene"/>
          <el-table-column label="主创人积分" align="center" width="85" prop="firstPoints"/>
          <el-table-column label="成员积分" align="center" width="80" prop="secondPoints"/>
          <el-table-column label="管控要求" align="center" prop="requireDesc"/>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-check"
                @click="handleChoose(scope.row)"
              >选择
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total1>0"
          :total="total1"
          :page.sync="querySceneParams.pageNum1"
          :limit.sync="querySceneParams.pageSize1"
          @pagination="getSceneList"
        />

      </el-form>

      <hr style="margin: 20px 0; border: none; border-top: 1px solid #eee;"/>

      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="奖项名称:" prop="name" label-width="100px">
              {{ this.form.name }}
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="项目参与角色" prop="isFirstPerson" label-width="100px">
              <el-select v-model="form.isFirstPerson" placeholder="请选择项目参与角色">
                <el-option
                  v-for="dict in dict.type.is_first_person"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="图片" prop="pic" label-width="100px">
              <image-upload v-model="form.pic"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="简介" prop="description" label-width="100px">
              <el-input v-model="form.description"
                        :rows="8"
                        type="textarea" placeholder="请输入内容"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 数据导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <div class="el-upload__tip" slot="tip" disabled="true">
            <el-checkbox v-model="upload.updateSupport"/>
            是否更新已经存在的用户数据
          </div>
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;"
                   @click="importTemplate">下载模板
          </el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listAwards, getAwards, delAwards, addAwards, updateAwards, importAwards} from "@/api/project/awards";
import {addCheckAwards, seeAwardsCheck, seeCheck} from "@/api/project/checkList";
import {listAwardsRule} from "@/api/project/awardsRule";
import {getToken} from "@/utils/auth";

export default {
  name: "Awards",
  dicts: ['is_first_person', 'project_state', 'project_line', 'project_prize', 'check_result', 'team_name', 'awards_type'],
  data() {
    return {
      checkResult: null,
      // 遮罩层
      loading: true,
      loading1: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      showSearch1: true,
      // 总条数
      total: 0,
      total1: 0,
      // 奖项管理表格数据
      awardsList: [],
      // 具体场景列表
      sceneList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      detailOpen: false,
      seeCheckOpen: false,
      //多选菜单
      menuExpand: false,
      menuNodeAll: false,
      defaultProps: {
        children: "children",
        label: "label"
      },
      currentNodeKey: null,

      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: {Authorization: "Bearer " + getToken()},
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/project/awards/importAwards"
      },
      // 查询参数
      querySceneParams: {
        pageNum1: 1,
        pageSize1: 10,
        type: null,
      },


      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        type: null,
        points: null,
        userId: null,
        checker1: null,
        checker2: null,
        lastCheckTime: null,
        pic: null,
        state: null,
        description: null,
        awardsId: null
      },
      // 表单参数
      form: {},
      checkForm: {},
      // 表单校验
      rules: {
        name: [
          {required: true, message: "奖项名称不能为空", trigger: "blur"}
        ],
        description: [
          {required: true, message: "简介不能为空", trigger: "blur"}
        ],
        isFirstPerson: [
          {required: true, message: "项目参与角色不能为空", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", {dangerouslyUseHTMLString: true});
      this.getList();
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('project/awards/importTemplate', {}, `awards_template_${new Date().getTime()}.xlsx`)
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },

    getActiveStep(result) {
      if (result === null) {
        return 2; // “审核中”步骤
      } else if (result === 1) {
        return 3; // “审核通过”步骤
      } else if (result === 0) {
        return 3; // “审核不通过”步骤
      }
      return 2; // 默认步骤
    },
    getStepTitle(result, stepIndex) {
      switch (stepIndex) {
        case 0:
          return '待审核';
        case 1:
          return result === 1 ? '审核通过' : (result === 0 ? '审核不通过' : '审核中');
        case 2:
          return '完成';
        default:
          return '';
      }
    },
    /** 查询奖励列表*/
    getSceneList() {
      this.loading1 = true;
      listAwardsRule(this.querySceneParams).then(response => {
        this.sceneList = response.rows;
        this.total1 = response.total;
        this.loading1 = false;
      });
    },
    /** 查询奖项管理列表 */
    getList() {
      this.loading = true;
      listAwards(this.queryParams).then(response => {
        this.awardsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleChoose(row) {
      this.form.name = row.scene;
      this.form.awardsId = row.id;
      this.form.type = row.type;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.detailOpen = false;
      this.seeCheckOpen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.checkForm = {
        checker1Name: undefined,
        checker2Name: undefined,
        checker1Id: null,
        checker2Id: null,
        result1: null,
        result2: null,
        checkResult: null,
        suggestion1: null,
        suggestion2: null,
        checker_time1: null,
        checker_time2: null
      };
      this.form = {
        id: null,
        name: null,
        type: null,
        points: null,
        createTime: null,
        userId: null,
        checker1: null,
        checker2: null,
        lastCheckTime: null,
        pic: null,
        state: null,
        description: null,
        awardsId: null,
        isFirstPerson: null
      };
      this.resetForm("form");
      this.resetForm("querySceneForm");
    },

    /** 搜索按钮操作 */
    handleQuery1() {
      this.querySceneParams.pageNum1 = 1;
      this.getSceneList();
    },
    /** 重置按钮操作 */
    resetQuery1() {
      this.resetForm("querySceneForm");
      this.handleQuery1();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.getSceneList();
      this.open = true;
      this.title = "添加奖项";
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "批量奖项导入";
      this.upload.open = true;
    },
    /** 查看审核详情 */
    handSeeCheck(row) {
      this.reset();
      const id = row.id || this.ids
      seeAwardsCheck(id).then(response => {
        this.checkForm = response.data;
        this.seeCheckOpen = true;
        this.title = "查看审核详情";
      });
    },
    /** 送审  */
    handleCheck(row) {
      this.$modal.confirm('是否确认送审').then(function () {
        return addCheckAwards(row);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("送审成功");
      }).catch(() => {
      });
      this.reset();
    },
    /** 查看详情 */
    handleDetail(row) {
      this.reset();
      const id = row.id || this.ids
      getAwards(id).then(response => {
        this.form = response.data;
        this.detailOpen = true;
        this.title = "查看详情";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAwards(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改奖项";
      });
    },
    // 操作人1
    check1Format(row, column) {
      return this.selectDictLabel(this.dict.type.team_name, row.checker1Id);
    },
    // 操作人2
    check2Format(row, column) {
      return this.selectDictLabel(this.dict.type.team_name, row.checker2Id);
    },
    result1Format(row, column) {
      return this.selectDictLabel(this.dict.type.check_result, row.result1);
    },
    result2Format(row, column) {
      return this.selectDictLabel(this.dict.type.check_result, row.result2);
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAwards(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAwards(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除？').then(function () {
        return delAwards(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('project/awards/export', {
        ...this.queryParams
      }, `awards_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style>
.custom-steps {
  width: 100%; /* 确保步骤条宽度为 100% */
  display: flex; /* 使用 Flexbox 进行布局 */
  justify-content: space-between; /* 确保步骤条项之间均匀分布 */
}
</style>
